$(function () {
    let layer = layui.layer
    initArtCateList()
    /* 1 */
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates'
        }).then(({
            data: res
        }) => {
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            let arr = []
            res.data.forEach(ele => {
                arr.push(`
                <tr>
                        <td>${ele.Id}</td>
                        <td>${ele.name}</td>
                        <td>${ele.alias}</td>
                        <td>
                            <button  data-id="${ele.Id}"  type="button" class="layui-btn layui-btn-xs btn-edit">修改</button>
                            <button data-id="${ele.Id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger btn-del">删除</button>
                        </td>
                    </tr>
                `)
            });
            $('tbody').empty().html(arr.join(''))
        })
    }

    /* 2 :弹出添加框加渲染到页面上去 */
    let indexAdd = 0
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '250px'],
            content: `
            <form id="addForm" class="layui-form"  action="">
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入名称" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类别名</label>
                <div class="layui-input-block">
                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入别名" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
    
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>`
        })
    })
    /* 3 */
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()

        axios({
                method: 'POST',
                url: '/my/article/addcates',
                data: $(this).serialize()
            }


        ).then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('恭喜您,添加成功')
            initArtCateList()
            layer.close(indexAdd)

        })
    })

    /* 4 修改文章弹出框并赋值加渲染弹出框 */
    let indexEdit = 0
    $('tbody').on('click', '.btn-edit', function (e) {
        indexEdit = layer.open({
            type: 1,
            title: '修改文章类别',
            area: ['500px', '250px'],
            content: `
            <form id="editForm" class="layui-form" lay-filter="editForm" action="">
            <input type="hidden" name="Id">
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入名称" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类别名</label>
                <div class="layui-input-block">
                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入别名" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
    
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                    
                </div>
            </div>
        </form>`
        })

        let id = $(e.target).attr('data-id')


        /* 发送axios并渲染 */
        axios({
            method: 'GET',
            url: '/my/article/cates/' + id
        }).then(({
            data: res
        }) => {
            /* console.log(res); */
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layui.form.val('editForm', res.data)


        })
    })
    /* 5:修改文章并渲染到页面 */
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()

        axios({
                method: 'POST',
                url: '/my/article/updatecate',
                data: $(this).serialize()
            }


        ).then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('恭喜您,修改成功')
            initArtCateList()
            layer.close(indexEdit)

        })
    })

    /* 删除出现弹框 */
    $('tbody').on('click', '.btn-del', function (e) {
        let id = $(e.target).attr('data-id')

        layer.confirm('确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            axios({
                url:'/my/article/deletecate/'+id
            }).then(({data:res}) => {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                layer.msg('删除成功')
                initArtCateList()

            })

            layer.close(index);
        });
    })
})